package org.iteam.bank.mina.client;

import java.util.Arrays;

import org.iteam.bank.mina.client.IoSessionFactory;
import org.iteam.commons.codec.AESCoder;
import org.iteam.commons.codec.HMACCoder;
import org.iteam.mina.message.BaseMessage;

public class ResultMessage extends BaseMessage {
	/**
	 * 
	 */
	private static final long serialVersionUID = -695119821123647909L;
	protected int result = 0xFF;// 操作结果

	public ResultMessage(BaseMessage res) {
		super();
		this.length = res.getLength();
		this.cmd = res.getCmd();
		this.rule = res.getRule();
		this.ver = res.getVer();
		this.seq = res.getSeq();
		this.token = res.getToken();
		this.content = res.getContent();
		this.verify = res.getVerify();
		this.result = res.getCmd() & 0x000000FF;
	}

	/**
	 * 是否成功
	 * 
	 * @return
	 */
	public boolean isSuccess() {
		return this.result == 0x00000000;
	}

	/**
	 * 是否失败
	 * 
	 * @return
	 */
	public boolean isFail() {
		return this.result == 0x00000001;
	}

	/**
	 * 系统结果处理中
	 * 
	 * @return
	 */
	public boolean isProcessing() {
		return this.result == 0x00000002;
	}

	/**
	 * 是否根据银行接口获取提示
	 * 
	 * @return
	 */
	public boolean isBankResult() {
		return this.result == 0x0000001F;
	}

	/**
	 * 是否根据[农商行子网关]银行接口获取提示
	 * 
	 * @return
	 */
	public boolean isRCBBankResult() {
		return this.result == 0x00000050;
	}

	/**
	 * 效验数据
	 * 
	 * @return
	 */
	public boolean verify() throws Exception {
		byte[] _verify = HMACCoder.encryptHMAC(this.content, IoSessionFactory.KEY_HMAC);
		return Arrays.equals(_verify, verify);
	}

	public boolean verify(String key) throws Exception {
		byte[] _verify = HMACCoder.encryptHMAC(this.content, key);
		return Arrays.equals(_verify, verify);
	}

	/**
	 * 解密数据
	 */
	public void decrypt() throws Exception {
		content = AESCoder.decrypt(content, IoSessionFactory.KEY_HMAC);
	}

	public void decrypt(String key) throws Exception {
		content = AESCoder.decrypt(content, key);
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

}
